package com.cci.kangdao.service.impl;

import com.cci.kangdao.dao.InsiteSupportDao;
import com.cci.kangdao.dao.model.InsiteSupportT;
import com.cci.kangdao.service.InsiteSupportService;
import com.cci.kangdao.service.PropertiesUtils;
import com.cci.kangdao.utilTool.AESTool;
import com.cci.kangdao.utilTool.ApiErrorCode;
import com.cci.kangdao.utilTool.ParameterTool;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections.MapUtils;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * INSITE支持相关的接口
 * 
 * @author fengshuai
 *
 */
@Service("insiteSupportService")
public class InsiteSupportServiceImpl implements InsiteSupportService {
	private Logger log = Logger.getLogger(this.getClass().getName());

	@Autowired
	private InsiteSupportDao insiteSupportDao;
	@Resource
	private PropertiesUtils propertiesUtils;

	/**
	 * App用户获取Insite支持接口
	 */
	@Override
	public ModelAndView getInsite(Map<String, Object> map) {
		try {
			HttpServletResponse response = (HttpServletResponse) map.get("response");
			HttpServletRequest request = (HttpServletRequest) map.get("request");
			JSONObject obj = new JSONObject();
			/**
			 * 判断参数是否为空
			 */
			if (map.get("insiteType") == null || map.get("insiteType").toString().equals("")) {
				obj.put("status", -5);
				obj.put("msg", ApiErrorCode.getInstance().getErrorMsg(-5));

				// 返回客户
				ParameterTool.writeResponse(response, obj.toString());
				obj = null;
				return null;
			}
			if (map.get("searchTxt") != null && !map.get("searchTxt").toString().equals("")) {
				map.put("searchTxt", "%" + map.get("searchTxt") + "%");
				map.remove("insiteType"); // 删除insiteType
			}
			int page = 1;
			if (map.get("page") != null) {
				page = MapUtils.getInteger(map, "page", 1);
			}
			int pagesize = Integer.valueOf(this.propertiesUtils.getPropertiesValue("pagesize"));
			// 默认情况下不传递参数
			obj.put("status", 0);
			obj.put("msg", "");
			JSONObject content;
			JSONArray jsonArray = new JSONArray();
			// 0安装指南，1最新公告，2功能介绍 3常见问题 搜索框
			/*
			 * { "status": 0, "msg": "", "insites": [{ "insiteName": "Insite名称",
			 * "isLogo": 0, "insiteType": 1, "fileUrl": "http:///2x.pdf" }] }
			 */
			PageHelper.startPage(page, pagesize);
			List<InsiteSupportT> insiteTypeDatas = insiteSupportDao.getInsiteTypeDatas(map);
			if (insiteTypeDatas != null) {
				for (InsiteSupportT insiteSupportT : insiteTypeDatas) {
					content = new JSONObject();
					content.put("id", AESTool.encryptString(insiteSupportT.getID().toString()));
					content.put("insiteName", insiteSupportT.getInsiteName());
					content.put("isLogo", insiteSupportT.getIsLogo());
					content.put("insiteType", insiteSupportT.getInsiteType());
					content.put("fileUrl", insiteSupportT.getFileUrl());
					jsonArray.put(content);
				}
			}
			obj.put("insites", jsonArray);
			ParameterTool.writeResponse(response, obj.toString());
			obj = null;
		} catch (Exception e) {
			// TODO: handle exception
			log.error(e.getMessage(), e);
		}
		return null;
	}

}
